﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class Clients_Control_New : System.Web.UI.UserControl
{

    private String strLiDiv = "<li> <a href='" + Constant.ROOT_PATH + "$link'>$text</a></li>";
    private String strDetail = "<p><b>$title</b></p><br/>$body";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            LoadAbout();
            String strPage = "";
            try
            {
                strPage = Request.Params["Pro"].ToString().Trim();
            }
            catch
            {

            }
            lblContentTitle.Text = Constant.ContentTitle(strPage);
        }
    }

    private void LoadAbout()
    {
        int id = 1;
        try
        {
            id = int.Parse(Request.Params["id"].ToString());
        }
        catch
        {
        }
      
        using (DamiWebDataContext db = new DamiWebDataContext())
        {
            int isActive = 1;
            int iCat = 2;
            WebData data = new WebData();
            DataLibrary.tbl_News_Lang item = data.tbl_News_Langs.GetListCache("New", Constant.time).SingleOrDefault(p => p.IdNew == id && p.IdLang == 1);
            String url = "";
            if (item != null)
            {
                url = url + strDetail.Replace("$title", item.TitleNew).Replace("$body", item.BodyNew);
                DataLibrary.tbl_News newItem = data.tbl_Newss.GetListCache("News", Constant.time).SingleOrDefault(p => p.IdNew == item.IdNew);
                if (newItem != null) {
                    isActive = newItem.IsActive;
                    iCat = newItem.IdCat;
                }
            }
            lblDetail.Text = url;
            var news = from p in db.tbl_News_Langs
                       where p.idLang == 1 && p.idNew < id && p.tbl_New.idNew == p.idNew 
                        && iCat==p.tbl_New.idCat && isActive==p.tbl_New.isActive
                       orderby p.tbl_New.orderId descending,p.idNew descending
                       select p;

            int l = news.Count<tbl_News_Lang>();
            int mautin = -1;
            try
            {
                mautin = Convert.ToInt32(Constant.View);
            }
            catch { }
            mautin = mautin == -1 ? 15 : mautin;
            url = "";
            List<tbl_News_Lang> list = news.ToList<tbl_News_Lang>();
            for (int i = 0; i < l; i++)
            {
                try
                {
                    if (!string.IsNullOrEmpty(list[i].ToString()))
                    {
                        String str = String.Format("New/{0}/{1}", list[i].idNew.ToString(), list[i].titleNew.ToString().
                            Replace(" ", "-").Replace(".", "").Replace("/", "_").Replace(":","").Replace("\"",""));
                        url = url + strLiDiv.Replace("$text", list[i].titleNew).Replace("$link", str);
                    }
                }
                catch { }
                if (i >=mautin)
                    break;
            }

            ltlTintuc.Text = url;
            if (string.IsNullOrEmpty(url))
            {
                lbl.Visible = false;
            }
            else
            {
                lbl.Visible = true;
            }
        }

    }
}